package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.user.User;
import com.qingcheng.service.user.UserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class UserController {
    @Reference
    private UserService userService;

    @RequestMapping("/register")
    public String register(){
        return "register";
    }
    /*
    * 发送验证码
    * @param phone
    * */
    @RequestMapping("/sendSms")
    @ResponseBody
    public String sendSms(@RequestParam String phone){
        userService.sendSms(phone);
        return "success";
    }
    /*
    * 注册用户
    * @param user
    * @param code
    * @return
    * */
    @RequestMapping("/save")
    @ResponseBody
    public Result save(@RequestBody User user, String code){
        //密码加密
        BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
        String newpassword = encoder.encode(user.getPassword());
        user.setPassword(newpassword);
        userService.add(user,code);
        return new Result();
    }
}
